<template>
    <div>
        <el-table :data="userlist" border style="width: 100%">
            <el-table-column prop="uid" label="用户编号" width="180">
            </el-table-column>
            <el-table-column prop="username" label="用户名称" width="180">
            </el-table-column>
            <el-table-column prop="rolename" label="所属角色">
            </el-table-column>
            <el-table-column label="状态">
                <template slot-scope="item">
                    <div>
                        {{ item.row.status == 1 ? '启用' : '禁用' }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="item">
                    <div>
                        <el-button size="mini" @click="edit(item.row.uid)" type="primary" plain>编辑</el-button>
                        <el-button @click="del(item.row.uid)" size="mini" type="danger" plain>删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination @current-change="changePage" :page-size="pageSize" class="pagin" background layout="prev, pager, next" :total="count">
        </el-pagination>
    </div>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'
import {userDel } from '../../request/api'
export default {
    computed: {
        ...mapGetters({
            userlist: 'user/getUser',
            pageSize: 'user/getSize',
            count:'user/getCount'
        })
    },
    mounted() {
        this.getUserAction()
        this.getCount()
    },
    methods: {
        ...mapActions({
            getUserAction: 'user/getUserAction',
            getCount: 'user/getCountAction',
            changePageAction:'user/changePageAction'
        }),
        //封装切换页面事件
        changePage(n) { 
            this.changePageAction(n)
        },
        edit(id) {
            this.$emit('edit', id)
        },
        del(uid) {
            this.$confirm("你确定要删除这条数据吗?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    //调用删除接口
                    userDel({
                        uid,
                    }).then((res) => {
                        if (res.code == 200) {
                            this.$message.success(res.msg);
                            //重新调用列表接口
                            this.getUserAction()
                            //重新调用总数
                            this.getCount()
                        } else {
                            this.$message.error(res.msg);
                        }
                    });
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除",
                    });
                });
        }
    }
}
</script>

<style scoped>
.pagin{
    float: right;
}
</style>